home *** CD-ROM | disk | FTP | other *** search
/ Freelog 125 / Freelog_MarsAvril2015_No125.iso / Musique / Quod Libet / quodlibet-3.3.0-installer.exe / bin / _osx_support.pyc (.txt) < prev    next >
Python Compiled Bytecode  |  2014-12-31  |  11KB  |  344 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.7)
  3.  
  4. '''Shared OS X support functions.'''
  5. import os
  6. import re
  7. import sys
  8. __all__ = [
  9.     'compiler_fixup',
  10.     'customize_config_vars',
  11.     'customize_compiler',
  12.     'get_platform_osx']
  13. _UNIVERSAL_CONFIG_VARS = ('CFLAGS', 'LDFLAGS', 'CPPFLAGS', 'BASECFLAGS', 'BLDSHARED', 'LDSHARED', 'CC', 'CXX', 'PY_CFLAGS', 'PY_LDFLAGS', 'PY_CPPFLAGS', 'PY_CORE_CFLAGS')
  14. _COMPILER_CONFIG_VARS = ('BLDSHARED', 'LDSHARED', 'CC', 'CXX')
  15. _INITPRE = '_OSX_SUPPORT_INITIAL_'
  16.  
  17. def _find_executable(executable, path = None):
  18.     """Tries to find 'executable' in the directories listed in 'path'.
  19.  
  20.     A string listing directories separated by 'os.pathsep'; defaults to
  21.     os.environ['PATH'].  Returns the complete filename or None if not found.
  22.     """
  23.     if path is None:
  24.         path = os.environ['PATH']
  25.     paths = path.split(os.pathsep)
  26.     (base, ext) = os.path.splitext(executable)
  27.     if (sys.platform == 'win32' or os.name == 'os2') and ext != '.exe':
  28.         executable = executable + '.exe'
  29.     if not os.path.isfile(executable):
  30.         for p in paths:
  31.             f = os.path.join(p, executable)
  32.             if os.path.isfile(f):
  33.                 return f
  34.         
  35.         return None
  36.     return None
  37.  
  38.  
  39. def _read_output(commandstring):
  40.     '''Output from successful command execution or None'''
  41.     import contextlib as contextlib
  42.     
  43.     try:
  44.         import tempfile as tempfile
  45.         fp = tempfile.NamedTemporaryFile()
  46.     except ImportError:
  47.         fp = open('/tmp/_osx_support.%s' % (os.getpid(),), 'w+b')
  48.  
  49.     with contextlib.closing(fp) as fp:
  50.         cmd = "%s 2>/dev/null >'%s'" % (commandstring, fp.name)
  51.         if not os.system(cmd):
  52.             return fp.read().strip()
  53.         return None
  54.  
  55.  
  56. def _find_build_tool(toolname):
  57.     '''Find a build tool on current path or using xcrun'''
  58.     if not _find_executable(toolname) and _read_output('/usr/bin/xcrun -find %s' % (toolname,)):
  59.         pass
  60.     return ''
  61.  
  62. _SYSTEM_VERSION = None
  63.  
  64. def _get_system_version():
  65.     '''Return the OS X system version as a string'''
  66.     global _SYSTEM_VERSION
  67.     if _SYSTEM_VERSION is None:
  68.         _SYSTEM_VERSION = ''
  69.         
  70.         try:
  71.             f = open('/System/Library/CoreServices/SystemVersion.plist')
  72.         except IOError:
  73.             pass
  74.  
  75.         
  76.         try:
  77.             m = re.search('<key>ProductUserVisibleVersion</key>\\s*<string>(.*?)</string>', f.read())
  78.         finally:
  79.             f.close()
  80.  
  81.         if m is not None:
  82.             _SYSTEM_VERSION = '.'.join(m.group(1).split('.')[:2])
  83.         
  84.     return _SYSTEM_VERSION
  85.  
  86.  
  87. def _remove_original_values(_config_vars):
  88.     '''Remove original unmodified values for testing'''
  89.     for k in list(_config_vars):
  90.         if k.startswith(_INITPRE):
  91.             del _config_vars[k]
  92.             continue
  93.  
  94.  
  95. def _save_modified_value(_config_vars, cv, newvalue):
  96.     '''Save modified and original unmodified value of configuration var'''
  97.     oldvalue = _config_vars.get(cv, '')
  98.     if oldvalue != newvalue and _INITPRE + cv not in _config_vars:
  99.         _config_vars[_INITPRE + cv] = oldvalue
  100.     _config_vars[cv] = newvalue
  101.  
  102.  
  103. def _supports_universal_builds():
  104.     '''Returns True if universal builds are supported on this system'''
  105.     osx_version = _get_system_version()
  106.     if osx_version:
  107.         
  108.         try:
  109.             osx_version = tuple((lambda .0: pass)(osx_version.split('.')))
  110.         except ValueError:
  111.             osx_version = ''
  112.         
  113.  
  114.     if osx_version:
  115.         return bool(osx_version >= (10, 4))
  116.  
  117.  
  118. def _find_appropriate_compiler(_config_vars):
  119.     '''Find appropriate C compiler for extension module builds'''
  120.     if 'CC' in os.environ:
  121.         return _config_vars
  122.     cc = oldcc = None['CC'].split()[0]
  123.     if not _find_executable(cc):
  124.         cc = _find_build_tool('clang')
  125.     elif os.path.basename(cc).startswith('gcc'):
  126.         data = _read_output("'%s' --version" % (cc.replace("'", '\'"\'"\''),))
  127.         if 'llvm-gcc' in data:
  128.             cc = _find_build_tool('clang')
  129.         
  130.     if not cc:
  131.         raise SystemError('Cannot locate working compiler')
  132.     if cc != oldcc:
  133.         for cv in _COMPILER_CONFIG_VARS:
  134.             if cv in _config_vars and cv not in os.environ:
  135.                 cv_split = _config_vars[cv].split()
  136.             cv_split[0] = cc if cv != 'CXX' else cc + '++'
  137.             _save_modified_value(_config_vars, cv, ' '.join(cv_split))
  138.             continue
  139.         
  140.     return _config_vars
  141.  
  142.  
  143. def _remove_universal_flags(_config_vars):
  144.     '''Remove all universal build arguments from config vars'''
  145.     for cv in _UNIVERSAL_CONFIG_VARS:
  146.         if cv in _config_vars and cv not in os.environ:
  147.             flags = _config_vars[cv]
  148.             flags = re.sub('-arch\\s+\\w+\\s', ' ', flags)
  149.             flags = re.sub('-isysroot [^ \t]*', ' ', flags)
  150.             _save_modified_value(_config_vars, cv, flags)
  151.             continue
  152.     return _config_vars
  153.  
  154.  
  155. def _remove_unsupported_archs(_config_vars):
  156.     '''Remove any unsupported archs from config vars'''
  157.     if 'CC' in os.environ:
  158.         return _config_vars
  159.     if None.search('-arch\\s+ppc', _config_vars['CFLAGS']) is not None:
  160.         status = os.system("echo 'int main{};' | '%s' -c -arch ppc -x c -o /dev/null /dev/null 2>/dev/null" % (_config_vars['CC'].replace("'", '\'"\'"\''),))
  161.         if status:
  162.             for cv in _UNIVERSAL_CONFIG_VARS:
  163.                 if cv in _config_vars and cv not in os.environ:
  164.                     flags = _config_vars[cv]
  165.                     flags = re.sub('-arch\\s+ppc\\w*\\s', ' ', flags)
  166.                     _save_modified_value(_config_vars, cv, flags)
  167.                     continue
  168.     return _config_vars
  169.  
  170.  
  171. def _override_all_archs(_config_vars):
  172.     '''Allow override of all archs with ARCHFLAGS env var'''
  173.     if 'ARCHFLAGS' in os.environ:
  174.         arch = os.environ['ARCHFLAGS']
  175.         for cv in _UNIVERSAL_CONFIG_VARS:
  176.             if cv in _config_vars and '-arch' in _config_vars[cv]:
  177.                 flags = _config_vars[cv]
  178.                 flags = re.sub('-arch\\s+\\w+\\s', ' ', flags)
  179.                 flags = flags + ' ' + arch
  180.                 _save_modified_value(_config_vars, cv, flags)
  181.                 continue
  182.     return _config_vars
  183.  
  184.  
  185. def _check_for_unavailable_sdk(_config_vars):
  186.     '''Remove references to any SDKs not available'''
  187.     cflags = _config_vars.get('CFLAGS', '')
  188.     m = re.search('-isysroot\\s+(\\S+)', cflags)
  189.     if m is not None:
  190.         sdk = m.group(1)
  191.         if not os.path.exists(sdk):
  192.             for cv in _UNIVERSAL_CONFIG_VARS:
  193.                 if cv in _config_vars and cv not in os.environ:
  194.                     flags = _config_vars[cv]
  195.                     flags = re.sub('-isysroot\\s+\\S+(?:\\s|$)', ' ', flags)
  196.                     _save_modified_value(_config_vars, cv, flags)
  197.                     continue
  198.     return _config_vars
  199.  
  200.  
  201. def compiler_fixup(compiler_so, cc_args):
  202.     """
  203.     This function will strip '-isysroot PATH' and '-arch ARCH' from the
  204.     compile flags if the user has specified one them in extra_compile_flags.
  205.  
  206.     This is needed because '-arch ARCH' adds another architecture to the
  207.     build, without a way to remove an architecture. Furthermore GCC will
  208.     barf if multiple '-isysroot' arguments are present.
  209.     """
  210.     stripArch = stripSysroot = False
  211.     compiler_so = list(compiler_so)
  212.     if not _supports_universal_builds():
  213.         stripArch = stripSysroot = True
  214.     else:
  215.         stripArch = '-arch' in cc_args
  216.         stripSysroot = '-isysroot' in cc_args
  217.     if stripArch or 'ARCHFLAGS' in os.environ:
  218.         while True:
  219.             
  220.             try:
  221.                 index = compiler_so.index('-arch')
  222.                 del compiler_so[index:index + 2]
  223.             continue
  224.             except ValueError:
  225.                 break
  226.                 continue
  227.             
  228.  
  229.     if 'ARCHFLAGS' in os.environ and not stripArch:
  230.         compiler_so = compiler_so + os.environ['ARCHFLAGS'].split()
  231.     if stripSysroot:
  232.         while True:
  233.             
  234.             try:
  235.                 index = compiler_so.index('-isysroot')
  236.                 del compiler_so[index:index + 2]
  237.             continue
  238.             except ValueError:
  239.                 break
  240.                 continue
  241.             
  242.  
  243.     sysroot = None
  244.     if '-isysroot' in cc_args:
  245.         idx = cc_args.index('-isysroot')
  246.         sysroot = cc_args[idx + 1]
  247.     elif '-isysroot' in compiler_so:
  248.         idx = compiler_so.index('-isysroot')
  249.         sysroot = compiler_so[idx + 1]
  250.     if sysroot and not os.path.isdir(sysroot):
  251.         log = log
  252.         import distutils
  253.         log.warn("Compiling with an SDK that doesn't seem to exist: %s", sysroot)
  254.         log.warn('Please check your Xcode installation')
  255.     return compiler_so
  256.  
  257.  
  258. def customize_config_vars(_config_vars):
  259.     '''Customize Python build configuration variables.
  260.  
  261.     Called internally from sysconfig with a mutable mapping
  262.     containing name/value pairs parsed from the configured
  263.     makefile used to build this interpreter.  Returns
  264.     the mapping updated as needed to reflect the environment
  265.     in which the interpreter is running; in the case of
  266.     a Python from a binary installer, the installed
  267.     environment may be very different from the build
  268.     environment, i.e. different OS levels, different
  269.     built tools, different available CPU architectures.
  270.  
  271.     This customization is performed whenever
  272.     distutils.sysconfig.get_config_vars() is first
  273.     called.  It may be used in environments where no
  274.     compilers are present, i.e. when installing pure
  275.     Python dists.  Customization of compiler paths
  276.     and detection of unavailable archs is deferred
  277.     until the first extension module build is
  278.     requested (in distutils.sysconfig.customize_compiler).
  279.  
  280.     Currently called from distutils.sysconfig
  281.     '''
  282.     if not _supports_universal_builds():
  283.         _remove_universal_flags(_config_vars)
  284.     _override_all_archs(_config_vars)
  285.     _check_for_unavailable_sdk(_config_vars)
  286.     return _config_vars
  287.  
  288.  
  289. def customize_compiler(_config_vars):
  290.     '''Customize compiler path and configuration variables.
  291.  
  292.     This customization is performed when the first
  293.     extension module build is requested
  294.     in distutils.sysconfig.customize_compiler).
  295.     '''
  296.     _find_appropriate_compiler(_config_vars)
  297.     _remove_unsupported_archs(_config_vars)
  298.     _override_all_archs(_config_vars)
  299.     return _config_vars
  300.  
  301.  
  302. def get_platform_osx(_config_vars, osname, release, machine):
  303.     '''Filter values for get_platform()'''
  304.     macver = _config_vars.get('MACOSX_DEPLOYMENT_TARGET', '')
  305.     if not _get_system_version():
  306.         pass
  307.     macrelease = macver
  308.     if not macver:
  309.         pass
  310.     macver = macrelease
  311.     if macver:
  312.         release = macver
  313.         osname = 'macosx'
  314.         cflags = _config_vars.get(_INITPRE + 'CFLAGS', _config_vars.get('CFLAGS', ''))
  315.         if macrelease + '.' >= '10.4.' and '-arch' in cflags.strip():
  316.             machine = 'fat'
  317.             archs = re.findall('-arch\\s+(\\S+)', cflags)
  318.             archs = tuple(sorted(set(archs)))
  319.             if len(archs) == 1:
  320.                 machine = archs[0]
  321.             elif archs == ('i386', 'ppc'):
  322.                 machine = 'fat'
  323.             elif archs == ('i386', 'x86_64'):
  324.                 machine = 'intel'
  325.             elif archs == ('i386', 'ppc', 'x86_64'):
  326.                 machine = 'fat3'
  327.             elif archs == ('ppc64', 'x86_64'):
  328.                 machine = 'fat64'
  329.             elif archs == ('i386', 'ppc', 'ppc64', 'x86_64'):
  330.                 machine = 'universal'
  331.             else:
  332.                 raise ValueError("Don't know machine value for archs=%r" % (archs,))
  333.         if machine == 'i386' or sys.maxint >= 0x100000000L:
  334.             machine = 'x86_64'
  335.         
  336.     elif machine in ('PowerPC', 'Power_Macintosh'):
  337.         if sys.maxint >= 0x100000000L:
  338.             machine = 'ppc64'
  339.         else:
  340.             machine = 'ppc'
  341.     
  342.     return (osname, release, machine)
  343.  
  344.